﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace MWC
{
	/// <summary>
	/// Okay, maybe overkill, but it's nice to have a simple way
	/// to get the list of days for the conference that's xplatform
	/// </summary>
	public static class DaysManager
	{
        public static bool UpdateDays()
        {
            List<BL.Meeting> meetings = new List<BL.Meeting>();
            bool isError = false;
            NetcampWebProxy.Meeting[] result;
            try
            {
                NetcampWebProxy.WebProxy proxy = new NetcampWebProxy.WebProxy();
                proxy.Proxy = null;
                result = proxy.GetNetcampMeetings();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                isError = true;
                return isError;
            }
            
            
            for (int i = 0; i < result.Length; i++)
            {
                meetings.Add(new BL.Meeting(result[i].time, result[i].title, result[i].link, result[i].address, result[i].latitude, result[i].longitude));
            }
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            string filePath = Path.Combine(path, "meetings.bin");
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
            formatter.Serialize(stream, meetings);
            stream.Close();
            return false;
        }
		public static List<BL.Meeting> GetDays ()
		{
            List<BL.Meeting> meetings = new List<BL.Meeting>();
            IFormatter formatter = new BinaryFormatter();
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            string filePath = Path.Combine(path, "meetings.bin");
            Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            meetings = (List<BL.Meeting>)formatter.Deserialize(stream);
            stream.Close();
            return meetings;
		}
        public static BL.Meeting GetLastDay()
        {
            List<BL.Meeting> meetings = new List<BL.Meeting>();
            IFormatter formatter = new BinaryFormatter();
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            string filePath = Path.Combine(path, "meetings.bin");
            Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            meetings = (List<BL.Meeting>)formatter.Deserialize(stream);
            stream.Close();
            //tutaj mozna latwo zmienic date aby testowac np z dniem obecnego wydarzenia itp
            //meetings[0].time = new DateTime(2014, 2, 24);
            return meetings[0];
        }
        public static Boolean IsSpecificDayNews(int position)
        {
            if (position == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static void DownloadImageByteArray(int dpi)
        {
            NetcampWebProxy.WebProxy proxy = new NetcampWebProxy.WebProxy();
            proxy.Proxy = null;
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            string filePath = Path.Combine(path, "image.bin");
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            
            File.WriteAllBytes(filePath, proxy.ImageByteArray(dpi));
        }

        public static long GetTimeOfLastImage(int dpi)
        {
            NetcampWebProxy.WebProxy proxy = new NetcampWebProxy.WebProxy();
            proxy.Proxy = null;
            return ConvertToTimestamp(proxy.TimeOfImageFile(dpi));
        }

        public static byte[] GetImageByteArray()
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            string filePath = Path.Combine(path, "image.bin");

            return File.ReadAllBytes(filePath);
        }

        public static long ConvertToTimestamp(System.DateTime value)
        {
            long epoch = (value.Ticks - 621355968000000000) / 10000000;
            return epoch;
        }
	}
}